/*
 * Main.fx
 *
 * Created on 01.10.2009, 20:04:00
 */

package javafx179labs.nez.hr.gui;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx179labs.nez.hr.util.GetFreeId;
import javafx179labs.nez.hr.gui.Line.*;
import javafx179labs.nez.hr.gui.ForLines.*;
import javafx179labs.nez.hr.bean.Enterprise;
import javafx179labs.nez.hr.bean.Post;
import javafx179labs.nez.hr.bean.Task;
import javafx179labs.nez.hr.bean.Employe;
import javafx179labs.nez.hr.bean.Department;
import javafx.ext.swing.SwingTextField;
import javafx.ext.swing.SwingComboBoxItem;
import javafx.ext.swing.SwingComboBox;
import javafx.scene.control.Button;
import java.lang.Void;

var enterprise = new Enterprise();
//чудовищно :)
enterprise.setBase("/home/civil/study/javafx/javafx179/JavaFX179Labs/src/javafx179labs/nez/hr/data/");

def betweenl = 30;
def bheight:Float = 21;
def lbwidth:Float = 28;
def rbwidth:Float = 40;
function posts() {

    var posts: Group[];
    var flag: Boolean = true;

    var postLine = function(postObj: Post) {
        var setter = function (postName: String) {
            postObj.setName(postName);
            enterprise.takeBack(postObj);
        }

        optLine(postObj.getName(), setter, "del",
                                            function():Void {
                                                enterprise.remove(postObj);
                                                flag = not flag;
                                            })
    }

    var adder = function (postName: String): Void {
        var newPost = new Post((new GetFreeId(enterprise.getPosts())).get(), postName);
        enterprise.add(newPost);
        flag = not flag;
    }

    var init = function (run: Boolean): Group[] {
        posts = [makeAddLine(adder)];

        for (post in enterprise.getPosts()) {
            insert postLine(post) into posts
        }

        return posts

    }

    Group {
        content: bind setIntervals(init(flag), betweenl)
    }

}

function tasks() {

    var tasks: Group[];
    var flag: Boolean = true;

    var taskLine = function(taskObj: Task) {
        var del = function ():Void {
            enterprise.remove(taskObj);
            flag = not flag;
        };

        var task = staticLine(taskObj.getTask(), "del", del);

        var setPerformer = function (id: Object):Void {
            taskObj.setPerformer(id as Integer);
            enterprise.takeBack(taskObj);
        }

        var genPerformers = function () {
            for (pr in enterprise.getEmployes()) {
                SwingComboBoxItem {
                    text: pr.getName()
                    value: pr.getId()
                }

            }
        }

        var performer = makeLine(enterprise.getPerformerName(taskObj), genPerformers, setPerformer);

        Group {
            content: setIntervals([task, performer], betweenl)
        }
    }

    var adder = function (taskName: String): Void {
        var newTask = new Task((new GetFreeId(enterprise.getTasks())).get(), taskName);
        enterprise.add(newTask);
        flag = not flag;
    }

   var init = function (run: Boolean): Group[] {
        tasks = [makeAddLine(adder)];
        for (task in enterprise.getTasks()) {
            insert taskLine(task) into tasks
        }
        
        setIntervals(tasks, betweenl * 2);

        return tasks
    }

    Group {
        content: bind init(flag)
    }

}

function departments() {
    var deps: Group[];
    var flag: Boolean = true;

    //well done
    var depLine = function(depObj: Department) {
        var setter = function (depName: String) {
            depObj.setName(depName);
            enterprise.takeBack(depObj);
        }

        optLine(depObj.getName(), setter, "del",
                                            function():Void {
                                                enterprise.remove(depObj);
                                                flag = not flag;
                                            })
    }

    var adder = function (depName: String): Void {
        var newDep = new Department((new GetFreeId(enterprise.getDepartments())).get(), depName);
        enterprise.add(newDep);
        flag = not flag;
    }

    var init = function (run: Boolean): Group[] {
        deps = [makeAddLine(adder)];

        for (dep in enterprise.getDepartments()) {
            insert depLine(dep) into deps
        }

        return deps

    }

    Group {
        content: bind setIntervals(init(flag), betweenl)
    }
}

function employes() {

    var employes: Group[];
    var flag: Boolean = true;

    var employeLine = function (employe: Employe) {
        var save = function () {
            enterprise.takeBack(employe);
        }

        var setFirstName = function (newname: String) {
            employe.setFirstName(newname);
            save()
        }

        var setLastName = function (newname: String) {
            employe.setLastName(newname);
            save()
        }

        var setDepartment = function (id: Object) {
            employe.setDepartmentId(id as Integer);
            save()
        }

        var setPostId = function (id: Object) {
            employe.setPostId(id as Integer);
            save()
        }

        var del = function ():Void {
            enterprise.remove(employe);
            flag = not flag;
        }

        Group {
            content: setIntervals ([optLine(employe.getFirstName(), setFirstName, "del", del),
                                    makeLine(employe.getLastName(), setLastName),
                                    makeLine(Enterprise.getHisPostName(employe),
                                            function() {
                                                for (post in Enterprise.getPosts()) {
                                                    SwingComboBoxItem {
                                                        value: post.getId()
                                                        text: post.getName()
                                                    }
                                                }
                                            },
                                            setPostId),
                                    makeLine(Enterprise.getHisDepartmentName(employe),
                                            function():SwingComboBoxItem[] {
                                                    for (dep in enterprise.getDepartments()) {
                                                            SwingComboBoxItem {
                                                                value: dep.getId()
                                                                text: dep.getName()
                                                            }
                                                    }
                                            },
                                            setDepartment)],
                                            betweenl)
        }
    }

    var lastNameField = SwingTextField {
        columns: 27
    }
    var firsNameField = SwingTextField {
        columns: 27
    }
    var postsBox = SwingComboBox {
        width: 302;
        height: bheight
    }

    var depBox = SwingComboBox {
        width: 302;
        height: bheight
    }
    var f = bind firsNameField;
    var l = bind lastNameField;
    var pb = bind postsBox;

    var addEmpl = Group {
        content: bind setIntervals([
                Group {content:[firsNameField]},
                Group {content:[lastNameField]},
                Group {content:[postsBox]},
                Group {
                    content: [
                            depBox,
                            Button {
                                text: "add"
                                height: bheight
                                width: lbwidth
                                layoutX: 305
                                action: function () {
                                    var postId = postsBox.selectedItem.value;
                                    var depId = depBox.selectedItem.value;
                                    var newEmploye = new Employe((new GetFreeId(enterprise.getEmployes())).get(),
                                                                  firsNameField.text,
                                                                  lastNameField.text,
                                                                  depId as Integer,
                                                                  postId as Integer);
                                    enterprise.add(newEmploye);
                                    flag = not flag;
                                }
                            }
                    ]
                }
        ], betweenl)
    }

    var initAddEmpl = function (run: Boolean) {
        lastNameField.text = "имя";
        firsNameField.text = "фамилия";
        postsBox.items = for (post in Enterprise.getPosts()) {
                            SwingComboBoxItem {
                                value: post.getId()
                                text: post.getName()
                         }
        }
        depBox.items = for (dep in enterprise.getDepartments()) {
                           SwingComboBoxItem {
                               value: dep.getId()
                               text: dep.getName()
                           }
                        }
        return addEmpl
    }

    var init = function (run: Boolean): Group[] {
        var add = initAddEmpl(flag);
        employes = [add];
        
        for (emp in enterprise.getEmployes()) {
            insert employeLine(emp) into employes;
        }
        setIntervals(employes, betweenl * 4);
        return employes
    }

    Group {
        content: bind init(flag)
    }



}


var list = SwingComboBox {
    items: for (i in ["employes", "departments", "posts", "tasks"]) {
        SwingComboBoxItem {
            text: i
        }
    }
}

var empl = employes();
var dep = departments();
var pos = posts();
var tas = tasks();
var stg = Stage {
    scene: Scene {
        height: 900
        width: 400
        content: [
                list,
                Group {
                    translateY: 30
                    translateX: 15
                    content: []
                }
        ]
    }
}


function select (i: Integer) {
    (stg.scene.content[1] as Group).content[0] = [empl, dep, pos, tas][i];
    stg.scene.impl_inputMethodStateChange();
    stg
}
var test = Scene {};
test.impl_inputMethodStateChange();
var stage = bind select(list.selectedIndex);

stage






